
use "$jfqa_rep/22_Forcerank_setup.dta", clear

qui{
*merge forcerank data
joinby ticker year month day using "$jfqa_rep/Forcerank.dta"

*adjust returns for contest
forval i=1/12 {
	egen mean_lag_`i'=mean(lag`i'_ret), by(contest_id)
	replace lag`i'_ret=lag`i'_ret-mean_lag_`i'
	drop mean_lag_`i'
}

drop if missing(convexity_4)
drop if missing(convexity_8)
drop if missing(convexity_12)
drop if missing(fut1_ret)

collapse  points convexity_12 lag* fut*, by(ticker contest_id start)

forval i=1/12 {
drop if missing(lag`i'_ret)
}

sort start ticker

gen obs=_n

gen fit_expect_12=.
tsset obs
order obs

gen date_as_num = start
format date_as_num %12.0g

foreach i in 	20583 	20590 	20597 	20605 	20611 	20618 	20625 	20632 	20646 	20653 	20660 	20667 	20674 	20681 	20688 	20695 	20709 	20716 	20723 	20730 	20737 	20744 	20751 	20758 	20765 	20772 	20779 	20786 	20793 	20800 	20807 	20822 	20828 	20836 	20842 	20849 	20856 	20863 	20871 	20877 	20884 	20891 	20898 	20905 	20912 	20919 	20926 	20933 	20940 	20947 	20954 	20961 	20969 	20975 	20978 	20982 	20989 	20996 	21003 	21010 	21017 	21024 	21031 	21038 	21045 	21052 	21059 	21073 	21080 	21087 	21094 	21101 	21108 	21115 	21122 	21129 	21136 	21143 	21150 	21157 	21164 	21171 	21192 	21206 	21213 	21220 	21227 {
nl (points = {b0} + ///
	{b1}*(({b2}*lag1_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^2*lag2_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^3*lag3_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^4*lag4_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^5*lag5_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^6*lag6_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^7*lag7_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^8*lag8_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^9*lag9_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^10*lag10_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^11*lag11_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12) + ///
		({b2}^12*lag12_ret)/({b2}+{b2}^2+{b2}^3+{b2}^4+{b2}^5+{b2}^6+{b2}^7+{b2}^8+{b2}^9+{b2}^10+{b2}^11+{b2}^12)))  ///
		if start<(`i'+1), initial(b0 0 b1 1 b2 0.5) nolog vce(cluster start)

gen fit_expect =_b[/b0] + ///
	_b[/b1]*((_b[/b2]*lag1_ret)/(_b[/b2]+_b[/b2]^2+_b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2]^5+_b[/b2]^6+_b[/b2]^7+_b[/b2]^8+_b[/b2]^9+_b[/b2]^10+_b[/b2]^11+_b[/b2]^12) + ///
		(_b[/b2]^9*lag9_ret)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4+_b[/b2]^5+_b[/b2]^6+_b[/b2]^7+_b[/b2]^8+_b[/b2]^9+_b[/b2]^10+_b[/b2]^11+_b[/b2]^12) + ///
		(_b[/b2]^10*lag10_ret)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4+_b[/b2]^5+_b[/b2]^6+_b[/b2]^7+_b[/b2]^8+_b[/b2]^9+_b[/b2]^10+_b[/b2]^11+_b[/b2]^12) + ///
		(_b[/b2]^11*lag11_ret)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4+_b[/b2]^5+_b[/b2]^6+_b[/b2]^7+_b[/b2]^8+_b[/b2]^9+_b[/b2]^10+_b[/b2]^11+_b[/b2]^12) + ///
		(_b[/b2]^12*lag12_ret)/(_b[/b2]+_b[/b2]^2+_b[/b2]^3+_b[/b2]^4+_b[/b2]^5+_b[/b2]^6+_b[/b2]^7+_b[/b2]^8+_b[/b2]^9+_b[/b2]^10+_b[/b2]^11+_b[/b2]^12))   if start<(`i'+1)
	
replace fit_expect_12=fit_expect if start==`i'
drop fit_expect
}


drop if missing(fit_expect_12)

egen convex_std=std(convexity_12)
replace fut1_ret=fut1_ret*100
gen res_expect_12=points-fit_expect_12

egen ticker_group=group(ticker contest_id)
egen start_group=group(start)
sort start_group ticker_group
tsset ticker_group start_group
}

***TABLE 13***
xtfmb fut1_ret points, lag(4)
xtfmb fut1_ret fit_expect_12, lag(4)
xtfmb fut1_ret res_expect_12, lag(4)

***TABLE 14***
xtfmb points convex_std, lag(4)
xtfmb fit_expect_12 convex_std, lag(4)
xtfmb res_expect_12 convex_std, lag(4)


